package sy.controller;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.db.mysql.model.KsTm;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.SessionInfo;
import sy.services.KsTmServiceI;
import sy.util.ConfigUtil;

/**
 *  @author zxl
 */
@Controller
@RequestMapping("/ksTmController")
public class KsTmController extends BaseController {
	@Autowired
	public KsTmServiceI ksTmService;


	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return ksTmService.findbyByGrid(param);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(KsTm t , HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Json j = new Json();
		try {
			t.setCJRSTR(sessionInfo.getName());
			t.setCREATEDATE(new Date());
			ksTmService.insert(t);
			j.setSuccess(true);
			j.setMsg("保存成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(KsTm t , HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Json j = new Json();
		try {
			t.setXGRSTR(sessionInfo.getName());
			t.setMODIFYDATE(new Date());
			ksTmService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			ksTmService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/selectByid")
	@ResponseBody
	public KsTm selectByid(String id) {
		return ksTmService.selectByPrimaryKey(id);
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "/ksTm/ksTm";
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		KsTm ksTm  = new KsTm();
		ksTm.setID(UUID.randomUUID().toString());
		request.setAttribute("ksTm", ksTm);
	return "/ksTm/ksTmAdd";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(String id ,HttpServletRequest request) {
		KsTm ksTm = this.ksTmService.selectByPrimaryKey(id);
		request.setAttribute("ksTm", ksTm);
		return "/ksTm/ksTmEdit";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage")
	public String viewPage(String id ,HttpServletRequest request) {
		KsTm ksTm = this.ksTmService.selectByPrimaryKey(id);
		request.setAttribute("ksTm", ksTm);
		return "/ksTm/ksTmView";
	}
}
